
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
	<meta charset="UTF-8">
    <title>制定出库单</title><!--下面的模态框改了一点-->
    <link rel="stylesheet"  href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
   	<link rel="stylesheet" href="/css/stock/c/Warehousing_Application_Registration.css" />
   <style type="text/css">
    	
    </style>
</head>
<body style="padding: 15px 50px;background-color: #eeeeee;">
	<div class="panel-heading hhead">
		<nav aria-label="breadcrumb">
		  <ol class="breadcrumb">
		  	<span style="color: blue;">您正在做的业务是：</span>
		    <li class="breadcrumb-item"><a href="#">库存管理</a></li>
		    <li class="breadcrumb-item"><a href="#">出入库调度管理</a></li>
		    <li class="breadcrumb-item active" aria-current="page">制定出库单</li>
		  </ol>
		</nav>
	</div>
	<form id="searchForm">
		<input type="hidden" name="spayId" id="spayId" value="">
		<input type="hidden" name="register" id="register2" value="">
		<input type="hidden" name="registerTime" id="registerTime2" value="">
	</form>
    <div style="margin-bottom: 2px">
        <div style="display: inline;margin-left: 95%;">
            <button class="btn btn-primary" id="back">返回</button>
        </div>
    </div> <!-- 此div改用栅格来做 -->
    <div id="designListBox">
        <div style="text-align: center">
            <h3>出库调度单</h3>
        </div>
        <div style="padding: 30px">
           <table class="tbl1">
               <tr>
                   <td class="col1">出库单编号：</td>
                   <td class="col2" id="payId"></td>
               </tr>
               <tr>
                   <td class="col1">出库理由：</td>
                   <td class="col2" id="reason"></td>
                   <td class="col1">出库详情理由：</td>
                   <td class="col2" id="reasonexact"></td>
               </tr>
           </table>

            <table class="table table-bordered" style="margin: 15px auto;">
                <thead>
                    <tr>
                        <th>序号</th><th>产品名称</th><th>产品编号</th>
                        <th>应出库件数</th><th>已出库件数</th>
                        <th>调度</th>
                    </tr>
                </thead>
                <tbody id="template">

                </tbody>
            </table>

            <table class="tbl3">
            	<tr>
                    <td class="col1">应出库总件数：</td>
                    <td class="col2" id="Should">111</td>
                    <td class="col1">已出库总件数：</td> <td class="col2" id="already"></td>
               </tr>
            </table>
        </div>
    </div>
    
    <div class="modal fade" id="proRegistraModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
		<div class="modal-dialog modal-xl">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
						</div>
						<div class="modal-body">
						    <div style="margin-bottom: 2px">
						        <div style="display: inline;margin-left: 85%;">
						        	<button class="btn btn-primary" id="submit">提交</button>
									<%--<button class="btn btn-primary">返回</button>--%>
						        </div>
						    </div> <!-- 此div改用栅格来做 -->
						    <div id="designListBox2">
						        <div style="text-align: center">
						            <h3>出库调度单</h3>
						        </div>
						        <div style="padding: 30px">
						           <table class="tbl1">
						               <tr>
						                   <td class="col1">出库单编号：</td>
						                   <td class="col2" id="payId2"></td>
						                   <td class="col1">产品编号：</td>
						                   <td class="col2" id="productId"></td>
						               </tr>
						               <tr>
						                   <td class="col1">产品名称：</td>
						                   <td class="col2" id="productName"></td>
						                   <td class="col1">成本单价：</td>
						                   <td class="col2" id="costPrice"></td>
						               </tr>
						           </table>
						
						            <table class="table table-bordered" style="margin: 15px auto;">
						                <thead>
						                    <tr>
						                        <th>序号</th><th>库房名称</th><th>存储地址编号</th>
						                        <th>存储地址名称</th><th>存储单元简称</th><th>当前存储数量</th>
						                        <th>本次出库数量</th>
						                    </tr>
						                </thead>
										<tbody id="template2" style="display: none">
										<tr>
											<td id="serialNum"></td>
											<td id="warehouseName"></td>
											<td id="areaId"></td>
											<td id="areaName"></td>
											<td id="referred"></td>
											<td id="amount"></td>
											<td class="num"><input class="num-input" value=""></td>
										</tr>
										</tbody>
										<tbody id="warehouseTbl">

										</tbody>
						            </table>
						
						            <table class="tbl3">
						            	<tr>
						                    <td class="col1">应出库数：</td>
						                    <td class="col2" id="ShouldSum">111</td>
						                    <td class="col1">已出库数：</td> <td class="col2" id="alreadySum">222</td>
						                </tr>
						            	<tr>
						                    <td class="col1">登记人：</td>
						                    <td class="col2"><input type="text" class="txt_under_line" id="register"></td>
						                    <td class="col1">登记时间：</td> <td class="col2" id="registerTime">222</td>
						               </tr>
						                
						            </table>
						        </div>
						    </div>
    
						</div>
						<div class="modal-footer">
							<button id="btnConcel" class="btn btn-danger" data-bs-dismiss="modal">
								关闭<span class="glyphicon glyphicon-remove"></span>
							</button>
						</div>
					</div>
				</div>
	</div>
		
		<script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
		<script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
		<script type="text/javascript">
			$(function (){
				// 初始化数据
				ItemApprovalTable();

				$("#back").on("click",function (){
					sessionStorage.clear();
					window.location.href = "/s-pay/Formulat_Prepare_delivery_order";
				});

				$("#proRegistraModal").on("hidden.bs.modal", function () {
					$("#warehouseTbl").empty();
				})

				$("#submit").on("click",function (){

					//本次出库数量
					let num1 = parseInt($("#warehouseTbl .num-input").val());

					//应出库数量
					let ShouldSum =parseInt($("#ShouldSum").text());
					let row = $("#warehouseTbl .num-input").parent().parent();
					//当前存储数量
					let storageNum = parseInt($(row).find("td:eq(5)").text());
					debugger;
					if(num1>storageNum || num1!=ShouldSum){
						$(row).find("td:eq(6) .num-input").val("");
						alert("库存总数量不足或出库数量有误");
						return;
					}

					//获取出库编号
					let id=$("#spayId").val();
					//获取出库单编号
					let payId = $("#payId2").text();
					let rows = $("#warehouseTbl tr");
					let productId=$("#productId").text();
					let productDescribe = $("#productDescribe").text();
					let num = rows.find(".num-input").val();
					if(num==""){
						alert("请输入本次出库数量,才能提交");
						return;
					}
					let data = [];
					data.push({
						id:id,
						//出库详情理由
						productDescribe: productDescribe,
						//出库单编号
						payId: payId,
						//产品编号
						productId: productId,
						//本次出库数量
						num:num
					})


					data=JSON.stringify(data);
					//调度
					$.ajax({
						url: "/s-pay/dispatch",
						type: "POST",
						data:  data,
						contentType: "application/json",
						dataType: "json",
						success: function (flag) {
							if (flag){

								window.location.href = "/s-pay/Prepare_delivery_order";
							}


						},
						error: function (e) {
						}
					})
				})


			})
			function ItemApprovalTable(){
				let id = sessionStorage.getItem("id");
				$("#id").val(id);
				$.ajax({
					url: "/s-pay/ItemApprovalTable?id="+id,
					type: "GET",
					dataType: "json",
					success: function (data) {
						console.log(data);
						if (data.errCode == "1") {
							alert(data.errMsg)
						} else {
							$("#payId").text(data.payId);
							$("#reason").text(data.reason);
							$("#reasonexact").text(data.reasonexact);
							$("#spayId").attr("value",data.id)
							$("#register2").attr("value",data.register)
							$("#registerTime2").attr("value",data.registerTime)
							// let a=$("#registerTime2").val()
							// alert(a);
							initialTemplate(data.id);

						}
					}
				})
			}
			function initialTotalSum(){
					// 初始化总和变量
					let totalOutbound = 0;
					let totalAlready = 0;
					// 选择表格中tbody下所有的tr元素，然后找到每个tr里面的第四列（索引为3，因为索引从0开始计数）
					$('#template tr').each(function () {
						let num = parseInt($(this).find('td').eq(3).text());
						let num2 = parseInt($(this).find('td').eq(4).text());
						totalOutbound += num;
						totalAlready += num2;
					});


					$("#Should").text(totalOutbound);
					$("#already").text(totalAlready);
			}
			function initialTemplate(parentId){
				$.ajax({
					url: "/s-pay/initialTemplate?id="+parentId,
					type: "GET",
					dataType: "json",
					success: function (data) {
						console.log(data);
						if (data.errCode == "1") {
							alert(data.errMsg)
						} else {
							let template = $("#template");
							// 清空表格
							template.empty();
							// 遍历数据并添加到表格中
							debugger;
							data.forEach(function (item, index) {
								let row = $("<tr>");
								row.append($("<td>").text(index + 1));
								row.append($("<td>").text(item.productName));
								row.append($("<td>").text(item.productId));
								row.append($("<td>").text(item.amount));
								row.append($("<td>").text("0"));
								if(item.payTag =="2"){
									row.append($("<td>").text("完成"));
								}else{
									row.append($("<td><a href='javascript:Add("  + item.id+ ")' class='btn btn-outline-success dispatch' >调度</a><input type='hidden' name='id' value='"+item.id+"'></td>"))
								}
								template.append(row);

							})
							//初始化应出库总件数和已出库总件数
							initialTotalSum();
						}
					}
				})
			}

			function Add(id){
				$.ajax({
					url: "/s-pay-details/initialSpayDetails?id=" + id,
					type: "get",
					datatype: "json",
					success: function (data) {
						// console.log(data)
						productId=data.productId;
						$('#productName').text(data.productName);
						$('#productId').text(data.productId);
						$("#costPrice").text(data.costPrice);
						$("#ShouldSum").text(data.amount);
						initialArchiveTable(data.productId);
						//得到当前存储量
						$.ajax({
							url: "/s-pay/initialAmount",
							type: "POST",
							data: data.productId,
							contentType: "application/json",
							dataType: "json",
							success: function (data) {
								console.log(data);
								if(data.sCell==null){
									alert("对不起，该产品未分配存储空间，请返回");
									$("#proRegistraModal").modal("hide");
								} else {
									$("#amount").text(data.sCell.amount);
									$('#proRegistraModal').modal('show')
								}

							},
							error: function (e) {
								console.log(e);
							}
						})
					}

				})
				let payId = $("#payId").text();
				$("#payId2").text(payId);
				$("#register").val($("#register2").val());
				$("#registerTime").text($("#registerTime2").val());
				$("#alreadySum").text("0");


				return false;
			}
			//添加模态框中库房表数据
			function initialArchiveTable(productId){
				$.ajax({
					url: "/s-pay/initialStoreroomTable?id="+productId,
					type: "GET",
					contentType: "application/json",
					dataType: "json",
					success: function (data) {
						console.log(data);
						$("#warehouseTbl").empty();
						let template = $("#template2").children();

						let serialNum = 1;
						$.each(data,function (i,aa) {
									let tr = template.clone();
									$.each(aa,function (name,value) {
										if(name == "ssecondProductArea"){
											tr.find("#warehouseName").text(aa["ssecondProductArea"]["firstProductArea"]["swarehouseSetting"]["warehouseName"]);
											let firstId=aa["ssecondProductArea"]["firstProductArea"]["firstId"];
											let secondId=aa["ssecondProductArea"]["secondId"];
											let thirdId=aa["thirdId"];
											tr.find("#areaId").text(firstId + "-" + secondId + "-" +thirdId);

											let firstName = aa["ssecondProductArea"]["firstProductArea"]["firstName"];
											let secondName = aa["ssecondProductArea"]["secondName"];
											let thirdName = aa["thirdName"];
											tr.find("#areaName").text(firstName + "-" + secondName + "-" + thirdName);

										}
									})
									tr.find("#serialNum").text(serialNum++);
									$("#warehouseTbl").append(tr);
						})
					},
					error: function (e) {

					}
				})

			}
		</script>
</body>
</html>
